package com.zy.cloud.controller;

import com.zy.cloud.entity.CommonResult;
import com.zy.cloud.entity.User;
import com.zy.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-03-01 20:26:15
 */
@RestController
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Value("${server.port}")
    private String serverPort;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/user/{id}")
    public CommonResult<User> selectOne(@PathVariable("id") Long id) {

        try {
            User user = this.userService.queryById(id);
            return new CommonResult<User>(200, "查询成功,服务器端口=" + serverPort, user);
        } catch (Exception e) {
            return new CommonResult<User>(400, "查询失败");
        }
    }

    @PostMapping("/user")
    public CommonResult<User> insertUser(@RequestBody User user) {

        try {
            User insert = this.userService.insert(user);
            return new CommonResult<User>(200, "插入成功,服务器端口=" + serverPort, insert);
        } catch (Exception e) {
            return new CommonResult<User>(400, "插入失败");
        }
    }

}
